package com.edu.boot.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.edu.boot.entity.Users;
import com.edu.boot.enums.UserTypeEnum;
import com.edu.boot.mapper.UserMapper;
import com.edu.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, Users> implements UserService{
    @Autowired
    UserMapper userMapper;
    public IPage<Users> selectUserByGender(Page<Users> page, String str){
        return userMapper.selectUserByGender(page,str);
    }

    @Override
    public IPage<Users> pageByGender(Page<Users> page, QueryWrapper<Users> queryWrapper) {
        return userMapper.pageSelect(page,queryWrapper);
    }
    
    @Override
    public Users findByUsername(String loginName) {
        Users user = userMapper.selectOne(new LambdaQueryWrapper<Users>()
                                                 .eq(Users::getUsername, loginName));
        return user;
    }
    
    @Override
    public Map<Integer, String> getQyMap(){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>()
                .eq(Users::getPid, UserTypeEnum.qiye.getCode());
        Map<Integer, String> qyMap = Collections.emptyMap();
        List<Users> qyList = userMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(qyList)){
            qyMap = qyList.stream()
                    .collect(Collectors.toMap(Users::getUid, Users::getNickname));
        }
        return qyMap;
    }
    @Override
    public Map<Integer, String> getUserMap(){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>()
                .eq(Users::getPid, UserTypeEnum.yonghu.getCode());
        Map<Integer, String> userMap = Collections.emptyMap();
        List<Users> userList = userMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(userList)){
            userMap = userList.stream()
                    .collect(Collectors.toMap(Users::getUid, Users::getNickname));
        }
        return userMap;
    }
    
    @Override
    public Map<Integer, Users> getUserObjMap(){
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<Users>()
                .eq(Users::getPid, UserTypeEnum.yonghu.getCode());
        Map<Integer, Users> userMap = Collections.emptyMap();
        List<Users> userList = userMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(userList)){
            userMap = userList.stream()
                    .collect(Collectors.toMap(Users::getUid, item -> item));
        }
        return userMap;
    }
}
